/*LICENSE_BEGIN
 * ******************************************************************
 * Copyright 2006 by the Dainamite Robocup-Team at the
 * DAI-Labor, TU-Berlin (www.dai-labor.de)
 * 
 * Members of Dainamite are:
 * Holger Endert
 * Robert Wetzker
 * Thomas Karbe
 * Axel Heßler
 * Philippe Bütter
 * Felix Brossmann
 * 
 * Licensed under the GNU GPL (General Public License) Free Software 
 * License, Version 2. You may use this file only in compliance with 
 * the License. You may obtain a copy of the License at:
 * 
 * http://www.fsf.org/licensing/licenses/gpl.txt
 * ******************************************************************
LICENSE_END*/
package robocup.component.tactics;

import robocup.component.actions.TurnAction;
import robocup.component.geometry.Vektor;
import robocup.component.worldobjects.Ball;

/**
 * The {@link SearchBallState} is triggered when a player lost the ball.
 * It triggeres the algorithm that searchs the ball. This state is also the
 * default state triggered when no other state is possible 
 * (which should never ever happen!!).
 * If you see this state even so the player knows where the ball is, you might
 * probably have forgotten to define a state for a given world.
 */
public class SearchBallState extends AbstractState {

	/**
	 * SearchBallState always makes the player look towards the ball!
	 * 
	 * @param parent -
	 *            the StateEvaluation (parent) object
	 */
	public SearchBallState(StateEvaluation parent) {

		super(parent);
		this.state = STATES.SEARCH_BALL;
	}

	@Override
	public boolean preCondition() {

		return !checkPlayerHasBall();
	}

	/**
	 * Dummy....
	 */
	@Override
	protected double successProbability() {

		return 1;
	}

	/**
	 * Because this is a dummy state - the values for assessment are very low!
	 */
	@Override
	protected double successBenefit() {

		return (!getWorld().getBallRef().isReliable()) ? 0.95 : 0.00001;
	}

	@Override
	public void calculateMainAction() {
    Ball ball = this.getWorld().getBallRef();

    //if ball is reliable look to the ball
    if (ball.isReliable()) {
      double angleToBall = Vektor.normalize(this.getWorld()
          .getNextPosition(ball).sub(
              this.getWorld().getMyNextPosition()).getAngle()
          - this.getWorld().getBodyAngle());
      double turnAngle;
      turnAngle = this.getWorld().getSelfRef().getAngleForTurn(
          angleToBall);

      mainAction = new TurnAction(turnAngle);

    } else {

      //turn by 45.0 degree
      double angleToTurn = 45 - 10.0;
      double correctedTurnAngle = this.getWorld().getSelfRef()
          .getAngleForTurn(angleToTurn);

      mainAction = new TurnAction(correctedTurnAngle);
    }
  }

}
